﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.ThuQuy;
using BLL.ThuQuy;

namespace WindowsForms.ThuQuy
{
    public partial class FormBaoCao : Form
    {
        List<string> list = new List<string>();
        string maBaoCao;
        public FormBaoCao()
        {
            InitializeComponent();
            
        }
        private void LoadListViewBaoCao(List<BaoCao> listbc)
        {
            lvBaoCao.Items.Clear();
            foreach (var item in listbc)
            {
                ListViewItem lvitem = new ListViewItem(item.MaBaoCao);
                lvitem.SubItems.Add(item.TenBaoCao);
                
                string tienthu = String.Format("{0:0,0}",item.SoTienThu );
                string tienchi = String.Format("{0:0,0}", item.SoTienChi);
                lvitem.SubItems.Add(tienthu);
                lvitem.SubItems.Add(tienchi);
                lvitem.SubItems.Add(item.NgayLap.ToShortDateString());
                lvitem.SubItems.Add(item.TenNguoiLap);
                lvitem.SubItems.Add(item.NoiDung);
                lvBaoCao.Items.Add(lvitem);
            }
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            FormThemBaoCao frm = new FormThemBaoCao();
            frm.ShowDialog();
        }

        private void TimKiemtoolStripButton1_Click(object sender, EventArgs e)
        {
            FormTimKiemBaoCao frm = new FormTimKiemBaoCao();
            frm.FormClosed+=new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        private void lvBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvBaoCao.CheckedItems.Count; i++)
            {
                maBaoCao = lvBaoCao.CheckedItems[i].SubItems[0].Text;
                list.Add(maBaoCao);
            }
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {

            foreach (var item in list)
            
                if (BaoCao.Objects.Delete(p => p.MaBaoCao == item))
                
                    MessageBox.Show("Xóa thành công");
                    
                
                else
                    MessageBox.Show("Xóa thất bại");
                LoadListViewBaoCao(BaoCao.Objects.All().ToList());
            
                
            
        }

        private void CapNhatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                FormThemBaoCao frm = new FormThemBaoCao();
                FormThemBaoCao.mabaocao = maBaoCao;
                FormThemBaoCao.state = true;
                //foreach (ListViewItem lvitem in lvBaoCao.SelectedItems)
                //{
                //    frm.mtxtMaBaoCao.Text = lvitem.SubItems[0].Text;
                //    frm.mtxtMaBaoCao.ReadOnly = true;
                //    frm.txtTenBaoCao.Text = lvitem.SubItems[1].Text;
                //    frm.txtSoTienChi.Text = lvitem.SubItems[2].Text;
                //    frm.txtSoTienThu.Text = lvitem.SubItems[3].Text;
                //    frm.dtpNgayLap.Value = DateTime.Parse(lvitem.SubItems[4].Text);
                //    frm.cbbNguoiLapBaoCao.SelectedIndex = int.Parse(lvitem.SubItems[5].Text);
                //    frm.txtNoiDung.Text = lvitem.SubItems[6].Text;
                //}
                frm.ShowDialog();
            }
            catch (Exception)
            {

                MessageBox.Show("Chọn Báo Cáo");
            }
  
        }

        private void LoadtoolStripButton1_Click(object sender, EventArgs e)
        {
            LoadListViewBaoCao(BaoCao.Objects.All().ToList());
        }

        public void frm_FormClosed(object sender, EventArgs e)
        {            
            if(FormTimKiemBaoCao.state)
            {
                string dataSource= FormTimKiemBaoCao.data;
                List<BaoCao> dsbc = BaoCao.Objects.Find(p => p.TenBaoCao.Contains(dataSource)).ToList();
                if(dsbc.Count>0)
                {
                    LoadListViewBaoCao(dsbc);
                }
                else
                    MessageBox.Show("Không tồn tại báo cáo có tên " + dataSource);
            }
        }

        private void FormBaoCao_Load(object sender, EventArgs e)
        {

        }

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

        }

        private void XemChiTietToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormChiTietBaoCao frm = new FormChiTietBaoCao();
            foreach (ListViewItem lvitem in lvBaoCao.SelectedItems)
            {
                frm.lblTenBaoCao.Text = lvitem.SubItems[1].Text;
                frm.lblSoTienThu.Text = lvitem.SubItems[2].Text;
                frm.lblSoTienChi.Text = lvitem.SubItems[3].Text;
                frm.lblNgayLap.Text = lvitem.SubItems[4].Text;
                frm.txtNoiDung.Text = lvitem.SubItems[6].Text;
                frm.lblNguoiLap.Text = lvitem.SubItems[5].Text;
            }
            frm.ShowDialog();
        }

        private void ThoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            InBaoCao frm = new InBaoCao();
            frm.ShowDialog();
        }
      
    }
}
